package com.qxzn.app.page.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.qxzn.app.page.R;
import com.qxzn.app.util.DisplayUtil;

/**
 *
 */
public class FaceBgView extends View {
    private Context context;
    private Paint bgPaint;
    private Paint circlePaint;
    private float centerX;
    private float centerY;
    private float radius;
    private float paddingLeft = 30;
    private float paddingRight = 30;
    private int width;
    private int height;

    public FaceBgView(Context context) {
        this(context, null);
    }

    public FaceBgView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public FaceBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initParams(attrs);
    }

    private void initParams(AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FaceBgView);
        if (typedArray != null) {
            typedArray.recycle();
        }
        paddingLeft = DisplayUtil.dip2px(context, paddingLeft);
        paddingRight = paddingLeft;
//        setBackground(getResources().getDrawable(R.color.black_7F000000));
        initCirclePaint();
        initBgPaint();
    }

    private void initCirclePaint() {
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setDither(true);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(DisplayUtil.dip2px(context, 2));
        circlePaint.setColor(getResources().getColor(R.color.blue_38FFFD));
    }

    private void initBgPaint() {
        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setDither(true);
//        bgPaint.setStyle(Paint.Style.STROKE);
//        bgPaint.setStrokeWidth(DisplayUtil.dip2px(context, 2));
        bgPaint.setColor(getResources().getColor(R.color.blue_38FFFD));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        centerX = w * 0.5f;
        centerY = h * 0.4f;
        radius = (w - paddingLeft - paddingRight) / 2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas);

        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        int layerId = canvas.saveLayer(0, 0, canvasWidth, canvasHeight, null, Canvas.ALL_SAVE_FLAG);
        bgPaint.setColor(getResources().getColor(R.color.black_7F000000));
        canvas.drawRect(0, 0, canvasWidth, canvasHeight, bgPaint);
        //使用CLEAR作为PorterDuffXfermode绘制蓝色的矩形
        bgPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        canvas.drawCircle(centerX, centerY, radius, bgPaint);
        //最后将画笔去除Xfermode
        bgPaint.setXfermode(null);
        canvas.restoreToCount(layerId);
    }

    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(centerX, centerY, radius, circlePaint);
    }

    public float getCenterX() {
        return centerX;
    }

    public float getCenterY() {
        return centerY;
    }

    public float getRadius() {
        return radius;
    }

    public float getPaddingLeft1() {
        return paddingLeft;
    }

    public float getPaddingRight1() {
        return paddingRight;
    }

    public int getWidth1() {
        return width;
    }

    public int getHeight1() {
        return height;
    }
}
